package com.jijuxie.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jijuxie.common.core.R;
import com.jijuxie.system.domain.SysMessage;
import com.jijuxie.system.service.ISysMessageService;

import cn.dev33.satoken.stp.StpUtil;

@RestController
@RequestMapping("/system/message")
public class SysMessageController {

    @Autowired
    private ISysMessageService messageService;

    @GetMapping("/list")
    public R<Page<SysMessage>> list(
            @RequestParam(required = false) String type,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        Long userId = StpUtil.getLoginIdAsLong();
        return R.ok(messageService.getUserMessages(userId, type, pageNum, pageSize));
    }

    @PutMapping("/read/{messageId}")
    public R<Void> markRead(@PathVariable Long messageId) {
        messageService.markRead(messageId);
        return R.ok();
    }

    @PutMapping("/readAll")
    public R<Void> markAllRead() {
        Long userId = StpUtil.getLoginIdAsLong();
        messageService.markAllRead(userId);
        return R.ok();
    }

    @DeleteMapping("/{messageId}")
    public R<Void> delete(@PathVariable Long messageId) {
        messageService.deleteMessage(messageId);
        return R.ok();
    }

    @GetMapping("/unreadCount")
    public R<Long> getUnreadCount() {
        Long userId = StpUtil.getLoginIdAsLong();
        return R.ok(messageService.getUnreadCount(userId));
    }
} 